package com.xiaoshuidi.cloud.module.contract.controller.admin.contract;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ContractExtrasDataCreateReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ContractExtrasDataRespVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.ContractExtrasDataUpdateReqVO;
import com.xiaoshuidi.cloud.module.contract.convert.contract.ContractExtrasDataConvert;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.ContractExtrasDataDO;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractExtrasDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 合同杂费")
@RestController
@RequestMapping("/contract/extras-data")
@Validated
public class ContractExtrasDataController {

    @Resource
    private ContractExtrasDataService extrasDataService;

    @PostMapping("/create")
    @Operation(summary = "创建合同杂费")
    @PreAuthorize("@ss.hasPermission('contract:extras-data:create')")
    public CommonResult<Long> createExtrasData(@Valid @RequestBody ContractExtrasDataCreateReqVO createReqVO) {
        return success(extrasDataService.createExtrasData(createReqVO));
    }

    @PostMapping("/query-list")
    @Operation(summary = "查询合同杂费")
    @PreAuthorize("@ss.hasPermission('contract:extras-data:query')")
    public CommonResult<List<ContractExtrasDataRespVO>> getExtrasDatalist(@RequestParam("contractId") Long contractId) {
        List<ContractExtrasDataDO> extrasDatalist = extrasDataService.getExtrasDatalist(contractId);
        List<ContractExtrasDataRespVO> contractExtrasDataRespVOS = ContractExtrasDataConvert.INSTANCE.convertList(extrasDatalist);
        return success(contractExtrasDataRespVOS);
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同杂费")
    @PreAuthorize("@ss.hasPermission('contract:extras-data:update')")
    public CommonResult<Boolean> updateExtrasData(@Valid @RequestBody ContractExtrasDataUpdateReqVO updateReqVO) {
        extrasDataService.updateExtrasData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同杂费")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:extras-data:delete')")
    public CommonResult<Boolean> deleteExtrasData(@RequestParam("id") Long id) {
        extrasDataService.deleteExtrasData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同杂费")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('contract:extras-data:query')")
    public CommonResult<ContractExtrasDataRespVO> getExtrasData(@RequestParam("id") Long id) {
        ContractExtrasDataDO extrasData = extrasDataService.getExtrasData(id);
        return success(ContractExtrasDataConvert.INSTANCE.convert(extrasData));
    }


}
